/*********************************************************************/
/*                                                                   */
/* FILE :        wrtmp.cpp                                           */
/*                                                                   */
/* DESCRIPTION : LightWave 3D to DirectX Export                      */
/*                                                                   */
/* HISTORY :     Oct 29, 2000    written by Yoshiaki Tazaki          */
/*               Oct 02, 2001    last update                         */
/*                                                                   */
/* Copyright (C) 2000-2001, D-STORM, Inc.                            */
/*                                                                   */
/*********************************************************************/
#ifdef GCTP_USE_XFILEAPI
#include <gctp/vector.hpp>
#endif

#include "dxexp.h"

#ifndef GCTP_USE_XFILEAPI

#include "rmxftmpl.h"

static char *template_std[] = 
{
	"template Header {",
	" <3D82AB43-62DA-11cf-AB39-0020AF71E433>",
	" WORD major;",
	" WORD minor;",
	" DWORD flags;",
	"}",
	"",
	"template Vector {",
	" <3D82AB5E-62DA-11cf-AB39-0020AF71E433>",
	" FLOAT x;",
	" FLOAT y;",
	" FLOAT z;",
	"}",
	"",
	"template Coords2d {",
	" <F6F23F44-7686-11cf-8F52-0040333594A3>",
	" FLOAT u;",
	" FLOAT v;",
	"}",
	"",
	"template Matrix4x4 {",
	" <F6F23F45-7686-11cf-8F52-0040333594A3>",
	" array FLOAT matrix[16];",
	"}",
	"",
	"template ColorRGBA {",
	" <35FF44E0-6C7C-11cf-8F52-0040333594A3>",
	" FLOAT red;",
	" FLOAT green;",
	" FLOAT blue;",
	" FLOAT alpha;",
	"}",
	"",
	"template ColorRGB {",
	" <D3E16E81-7835-11cf-8F52-0040333594A3>",
	" FLOAT red;",
	" FLOAT green;",
	" FLOAT blue;",
	"}",
	"",
	"template IndexedColor {",
	" <1630B820-7842-11cf-8F52-0040333594A3>",
	" DWORD index;",
	" ColorRGBA indexColor;",
	"}",
	"",
	"template Boolean {",
	" <4885AE61-78E8-11cf-8F52-0040333594A3>",
	" WORD truefalse;",
	"}",
	"",
	"template Boolean2d {",
	" <4885AE63-78E8-11cf-8F52-0040333594A3>",
	" Boolean u;",
	" Boolean v;",
	"}",
	"",
	"template MaterialWrap {",
	" <4885AE60-78E8-11cf-8F52-0040333594A3>",
	" Boolean u;",
	" Boolean v;",
	"}",
	"",
	"template TextureFilename {",
	" <A42790E1-7810-11cf-8F52-0040333594A3>",
	" STRING filename;",
	"}",
	"",
	"template Material {",
	" <3D82AB4D-62DA-11cf-AB39-0020AF71E433>",
	" ColorRGBA faceColor;",
	" FLOAT power;",
	" ColorRGB specularColor;",
	" ColorRGB emissiveColor;",
	" [...]",
	"}",
	"",
	"template MeshFace {",
	" <3D82AB5F-62DA-11cf-AB39-0020AF71E433>",
	" DWORD nFaceVertexIndices;",
	" array DWORD faceVertexIndices[nFaceVertexIndices];",
	"}",
	"",
	"template MeshFaceWraps {",
	" <4885AE62-78E8-11cf-8F52-0040333594A3>",
	" DWORD nFaceWrapValues;",
	" Boolean2d faceWrapValues;",
	"}",
	"",
	"template MeshTextureCoords {",
	" <F6F23F40-7686-11cf-8F52-0040333594A3>",
	" DWORD nTextureCoords;",
	" array Coords2d textureCoords[nTextureCoords];",
	"}",
	"",
	"template MeshMaterialList {",
	" <F6F23F42-7686-11cf-8F52-0040333594A3>",
	" DWORD nMaterials;",
	" DWORD nFaceIndexes;",
	" array DWORD faceIndexes[nFaceIndexes];",
	" [Material]",
	"}",
	"",
	"template MeshNormals {",
	" <F6F23F43-7686-11cf-8F52-0040333594A3>",
	" DWORD nNormals;",
	" array Vector normals[nNormals];",
	" DWORD nFaceNormals;",
	" array MeshFace faceNormals[nFaceNormals];",
	"}",
	"",
	"template MeshVertexColors {",
	" <1630B821-7842-11cf-8F52-0040333594A3>",
	" DWORD nVertexColors;",
	" array IndexedColor vertexColors[nVertexColors];",
	"}",
	"",
	"template Mesh {",
	" <3D82AB44-62DA-11cf-AB39-0020AF71E433>",
	" DWORD nVertices;",
	" array Vector vertices[nVertices];",
	" DWORD nFaces;",
	" array MeshFace faces[nFaces];",
	" [...]",
	"}",
	"",
	"template FrameTransformMatrix {",
	" <F6F23F41-7686-11cf-8F52-0040333594A3>",
	" Matrix4x4 frameMatrix;",
	"}",
	"",
	"template Frame {",
	" <3D82AB46-62DA-11cf-AB39-0020AF71E433>",
	" [...]",
	"}",
	"",
	NULL,
};

static char *template_ext[] = 
{
	"template XSkinMeshHeader {",
	" <3cf169ce-ff7c-44ab-93c0-f78f62d172e2>",
	" WORD nMaxSkinWeightsPerVertex;",
	" WORD nMaxSkinWeightsPerFace;",
	" WORD nBones;",
	"}",
	"",
	"template VertexDuplicationIndices {",
	" <b8d65549-d7c9-4995-89cf-53a9a8b031e3>",
	" DWORD nIndices;",
	" DWORD nOriginalVertices;",
	" array DWORD indices[nIndices];",
	"}",
	"",
	"template SkinWeights {",
	" <6f0d123b-bad2-4167-a0d0-80224f25fabb>",
	" STRING transformNodeName;",
	" DWORD nWeights;",
	" array DWORD vertexIndices[nWeights];",
	" array FLOAT weights[nWeights];",
	" Matrix4x4 matrixOffset;",
	"}",
	"",
	NULL,
};

static unsigned char bin_template_ext[] = {
	0x0, 0x1F, 0x0, 0x1, 0x0, 0x0, 0x0, 0xF,
	0x58, 0x53, 0x6B, 0x69, 0x6E, 0x4D, 0x65, 0x73,
	0x68, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x0,
	0xA, 0x0, 0x5, 0xCE, 0x69, 0xF1, 0x3C, 0x7C,
	0xFF, 0xAB, 0x44, 0x93, 0xC0, 0xF7, 0x8F, 0x62,
	0xD1, 0x72, 0xE2, 0x0, 0x28, 0x0, 0x1, 0x0,
	0x0, 0x0, 0x18, 0x6E, 0x4D, 0x61, 0x78, 0x53,
	0x6B, 0x69, 0x6E, 0x57, 0x65, 0x69, 0x67, 0x68,
	0x74, 0x73, 0x50, 0x65, 0x72, 0x56, 0x65, 0x72,
	0x74, 0x65, 0x78, 0x0, 0x14, 0x0, 0x28, 0x0,
	0x1, 0x0, 0x0, 0x0, 0x16, 0x6E, 0x4D, 0x61,
	0x78, 0x53, 0x6B, 0x69, 0x6E, 0x57, 0x65, 0x69,
	0x67, 0x68, 0x74, 0x73, 0x50, 0x65, 0x72, 0x46,
	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0x22, 0x4F, 0x25, 0xFA, 0xBB, 0x0, 0x31,
	0x0, 0x1, 0x0, 0x0, 0x0, 0x11, 0x74, 0x72,
	0x61, 0x6E, 0x73, 0x66, 0x6F, 0x72, 0x6D, 0x4E,
	0x6F, 0x64, 0x65, 0x4E, 0x61, 0x6D, 0x65, 0x0,
	0x14, 0x0, 0x29, 0x0, 0x1, 0x0, 0x0, 0x0,
	0x8, 0x6E, 0x57, 0x65, 0x69, 0x67, 0x68, 0x74,
	0x73, 0x0, 0x14, 0x0, 0x34, 0x0, 0x29, 0x0,
	0x1, 0x0, 0x0, 0x0, 0xD, 0x76, 0x65, 0x72,
	0x74, 0x65, 0x78, 0x49, 0x6E, 0x64, 0x69, 0x63,
	0x65, 0x73, 0x0, 0xE, 0x0, 0x1, 0x0, 0x0,
	0x0, 0x8, 0x6E, 0x57, 0x65, 0x69, 0x67, 0x68,
	0x74, 0x73, 0x0, 0xF, 0x0, 0x14, 0x0, 0x34,
	0x0, 0x2A, 0x0, 0x1, 0x0, 0x0, 0x0, 0x7,
	0x77, 0x65, 0x69, 0x67, 0x68, 0x74, 0x73, 0x0,
	0xE, 0x0, 0x1, 0x0, 0x0, 0x0, 0x8, 0x6E,
	0x57, 0x65, 0x69, 0x67, 0x68, 0x74, 0x73, 0x0,
	0xF, 0x0, 0x14, 0x0, 0x1, 0x0, 0x0, 0x0,
	0x9, 0x4D, 0x61, 0x74, 0x72, 0x69, 0x78, 0x34,
	0x78, 0x34, 0x0, 0x1, 0x0, 0x0, 0x0, 0xC,
	0x6D, 0x61, 0x74, 0x72, 0x69, 0x78, 0x4F, 0x66,
	0x66, 0x73, 0x65, 0x74, 0x0, 0x14, 0x0, 0xB,
};

/*
 *
 *  WriteTemplates   - write XFile Templates
 *
 */

void dxExporter::WriteTemplates()
{
	switch (_options->format) {
	case DX_FORMAT_BINARY:
		BIN_WriteTemplates();
		break;
	case DX_FORMAT_TEXT:
		TXT_WriteTemplates();
		break;
	}
}

/*
 *
 *  TXT_WriteTemplates   - write XFile Templates
 *
 */

void dxExporter::TXT_WriteTemplates()
{
	char          **temp;

	if (_options->outputTemplates == TRUE)
	{
		temp = template_std;
		ffp( 0, "\n" );
		while (*temp) {
			ffp( 0, "%s\n", *temp++ );
		}
	}

	if (_options->version == DX_VERSION_DIRECTX8)
	{
		temp = template_ext;
		ffp( 0, "\n" );
		while (*temp) {
			ffp( 0, "%s\n", *temp++ );
		}
	}
}

/*
 *
 *  BIN_WriteTemplates   - write XFile Templates
 *
 */

void dxExporter::BIN_WriteTemplates()
{
	if (_options->outputTemplates == TRUE)
	{
		ffp_bytes( D3DRM_XTEMPLATES, D3DRM_XTEMPLATE_BYTES );
	}

	if (_options->version == DX_VERSION_DIRECTX8)
	{
		ffp_bytes( bin_template_ext, sizeof(bin_template_ext) );
	}
}

#endif
